from django.db import models
from django.contrib.auth.models import User

from user.manages import UserScoreManage


class UserInfo(models.Model):
    """
     用户基本信息表
     头像、 出生日期、 昵称、姓名、性别、  所属用户（关系属性）
    """
    avatar = models.ImageField(upload_to="avatar/", default="avatar/default.png", verbose_name="头像", db_comment="用户头像")

    birthday = models.DateField(verbose_name="出生日期", db_comment="用户出生日期", null=True, blank=True)

    nickname = models.CharField(max_length=32, verbose_name="昵称", db_comment="用户昵称", null=True, blank=True)

    name = models.CharField(max_length=32, verbose_name="姓名", db_comment="用户姓名", null=True, blank=True)

    gender = models.CharField(max_length=1, choices=(("M", "男"), ("F", "女"), ('S', '保密')),
                              verbose_name="性别", db_comment="用户性别", default="S")

    user = models.OneToOneField(to=User, on_delete=models.CASCADE, verbose_name="所属用户",
                                null=True, blank=True, related_name="info",
                                db_comment="用户所属用户")

    class Meta:
        db_table = "tb_user_info"


class ScoreConfig(models.Model):
    """
    积分配置表
    动作(注册、充值、上传、评论)，  赠送积分数量、 是否启用、 是否删除
    """
    action = models.CharField(max_length=32, verbose_name="动作", db_comment="积分动作", unique=True)

    score = models.IntegerField(verbose_name="赠送积分数量", db_comment="赠送积分数量", default=0)

    is_enable = models.BooleanField(verbose_name="是否启用", db_comment="是否启用", default=True)

    is_delete = models.BooleanField(verbose_name="是否删除", db_comment="是否删除", default=False)

    class Meta:
        db_table = "tb_score_config"
        verbose_name_plural = "积分配置"


class UserScore(models.Model):
    """
    用户积分表
    用户（关系属性）、积分、 积分来源、获取时间、 资源ID (不用外键关联)
    """
    score = models.IntegerField(verbose_name="积分", db_comment="用户积分", default=0)

    remark = models.CharField(max_length=100, verbose_name="积分来源", db_comment="积分来源")

    create_time = models.DateTimeField(verbose_name="获取时间", db_comment="积分获取时间", auto_now_add=True)

    user = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name="所属用户",
                                null=True, blank=True, related_name="score_list",
                                db_comment="用户所属用户")

    res_id = models.BigIntegerField(verbose_name="资源ID", db_comment="资源ID", null=True, blank=True, db_index=True)

    # 添加 自定义模型管理器
    objects = UserScoreManage(ScoreConfig)

    class Meta:
        db_table = "tb_user_score"



class UserBlack(models.Model):
    """
    用户黑名单表
    用户(关系属性) 、拉黑用户(关系属性) 、 录入时间 、
    """
    user = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name="所属用户",
                             null=True, blank=True, related_name="black_list",
                             db_comment="用户所属用户")

    black_user = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name="拉黑用户",
                                   null=True, blank=True, db_comment="被拉黑用户")

    create_time = models.DateTimeField(verbose_name="录入时间", db_comment="录入时间", auto_now_add=True)

    class Meta:
        db_table = "tb_user_black"
        unique_together = ("user", "black_user")